/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function (head) {
    if(head == null || head.next == null) {
        return head
    }
    let p = head
    let stack = []
    while(p) {
        if(stack.length == 0) {
            stack.push({
                key: p.val,
                nums: 1,
                node: p
            })
        } else {
            if(stack[stack.length - 1].key === p.val) {
                stack[stack.length - 1].nums++
            } else {
                stack.push({
                    key: p.val,
                    nums: 1,
                    node: p
                })
            }
        }
        p = p.next
    }
    let tmp = new ListNode()
    p = tmp
    stack.forEach(item => {
        if(item.nums == 1) {
            p.next = item.node
            p = p.next
        }
    })
    p.next = null
   return tmp.next
}; 
// 灵茶山
/*
哨兵节点：当需要删除头节点时，定义一个哨兵节点
*/
var deleteDuplicates = function (head) {
    let dummpyNode = new ListNode(0, head)
    let cur = dummpyNode
    while(cur.next !== null && cur.next.next !== null) {
        let val = cur.next.val
        if(cur.next.next.val == val) {
            while(cur.next !== null && cur.next.val == val) {
                cur.next = cur.next.next
            }
        }
    }
    return dummpyNode.next
}